package com.combat.text.user.service;


import com.combat.text.user.entity.root;
import com.combat.text.user.entity.user;
import com.combat.text.user.entity.userroot;
import com.combat.text.user.mapper.equipmentMapper;
import com.combat.text.user.mapper.rootMapper;
import com.combat.text.user.mapper.u_rMapper;
import com.combat.text.user.mapper.userMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Source;
import java.util.*;

/**
 * @author KangJx
 * @date 2020/12/15
 */
@Service
public class DeptService {

    @Autowired
    userMapper userMappe;
    @Autowired
    u_rMapper u_rMappe;
    @Autowired
    rootMapper rootMappe;
    @Autowired
    equipmentMapper  equipmentMapper;




    public List<root> showroot() {
        return  rootMappe.selectList(null);
    }

    public Map<String,Object> login(int uno,int upassword) {
        Map<String,Object> map = new HashMap<>();
            map.put("status", userMappe.find(uno,upassword)==null ? "no":"success");
            map.put("data",userMappe.find(uno,upassword));
        return  map;
    }

    public List<userroot> showrroot() {
        return  u_rMappe.select();
    }



    public int save(user u) {
        int a = userMappe.finduser(u.getUno()).isEmpty()? 0: 1;
        if(a==1){
            if("管理员".equals(u.post)){
             u.setPostid(1);
            }
            if(u.getPost().equals("操作员")){
                u.setPostid(2);
            }
            if("工作员".equals(u.post)){
                u.setPostid(3);
            }
           return userMappe.updateuser(u);
        }
        else {
            if("管理员".equals(u.post)){
                u.setPostid(1);
            }
            if(u.getPost().equals("操作员")){
                u.setPostid(2);
            }
            if("工作员".equals(u.post)){
                u.setPostid(3);
            }
            return userMappe.save(u);
        }
    }


    public int deluser(int uno) {
        return userMappe.deleteuser(uno);
    }


    public Map<String, Object> selectshow(int limit, int page, String search) {
        if(search==""||search==null){
            return  show(limit,page);
        }
        else {
            return findshow(search,limit,page);
        }

    }

    private Map<String, Object> findshow(String search, int limit, int page) {
        int start = (page-1)*limit;
        Map<String,Object > map= new HashMap<>();
        map.put("code", 0);
        map.put("msg", "您的操作已成功！！！");
        map.put("count", userMappe.findshow(start,limit,search).size());
        map.put("data",userMappe.findshow(start,limit,search));
        return  map;
    }

    public Map<String,Object> show(int limit, int page){
        int start = (page-1)*limit;
        Map<String,Object > map= new HashMap<>();
        map.put("code", 0);
        map.put("msg", "您的操作已成功！！！");
        map.put("count", userMappe.select(start,limit).size());
        map.put("data",userMappe.select(start,limit));
        return  map;
    }

    public  List<Map<String,Object>> watershow() {
        List<Map<String,Object>> list=new ArrayList<>();
        int ids[] = equipmentMapper.findeid();
        for (int i = 0; i < ids.length; i++) {
            Map<String,Object> map = new HashMap<>();
            String ename = equipmentMapper.findename(ids[i]);
            List<Integer> list1 = equipmentMapper.findnum(ids[i]);
            map.put("name",ename);
            map.put("num",list1);
            list.add(map);
        }
        return list;
    }
}
